//
//  WorldDetailViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "WorldDetailViewController.h"
#import "LSAppTools.h"
#import "WorldDetailTableViewCell.h"
#import "RoadViewController.h"
#import "TravelSubjectViewController.h"
#import "TravelDiaryViewController.h"
#import "SearchViewController.h"

@interface WorldDetailViewController ()<UITableViewDataSource, UITableViewDelegate, foreignDetailCellDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *myArray;
@end

@implementation WorldDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.foreignModel.name_zh_cn;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    
    UIImage *leftImage = [UIImage imageNamed:@"iconfont-fanhui-2.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    
    
    
//    UIImage *rightImage = [UIImage imageNamed:@"NavBar_Share_Normal.png"];
//    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction:)];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49 * HEI) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[WorldDetailTableViewCell class] forCellReuseIdentifier:@"WorldDetailTableViewCell"];
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myArray = [NSMutableArray array];
    
    [self getAFNetWorkingData];

}



- (void)getAFNetWorkingData{
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json", self.foreignModel.nameId];
    [LSAppTools getDataWithURL:url blcok:^(id result) {
        NSMutableArray *bigArray = [NSMutableArray arrayWithArray:result];
        for (NSMutableDictionary *dic in bigArray) {
            ForeignModel *foreignModel = [[ForeignModel alloc] init];
            [foreignModel setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:foreignModel];
            
        }
        [self.myTableView reloadData];
        
    }];
}

- (void)pushToViewControllerTag:(NSInteger)tag NameId:(NSString *)nameId Name:(NSString *)name
{
    
    
    if (tag == 1001) {
        
        RoadViewController *roadVC = [[RoadViewController alloc] init];
        
        [self.navigationController pushViewController:roadVC animated:YES];
        
        RoadModel *roadModel = [[RoadModel alloc] init];
        roadVC.roadModel = roadModel;
        
        roadModel.nameId = nameId;
        
        roadModel.name = name;
        
        
        
    } else if (tag == 1002){
        
        TravelSubjectViewController *subjectVC = [[TravelSubjectViewController alloc] init];
        
        RoadModel *roadModel = [[RoadModel alloc] init];
        
        roadModel.nameId = nameId;
        
        roadModel.name = name;
        
        subjectVC.dURL = nameId;
        
        [self.navigationController pushViewController:subjectVC animated:YES];
        
        
        
        
        
        
    } else {
        
        TravelDiaryViewController *diaryVC = [[TravelDiaryViewController alloc] init];
        
        RoadModel *roadModel = [[RoadModel alloc] init];
        
        diaryVC.roadModel = roadModel;
        
        roadModel.name = name;
        
        roadModel.nameId = nameId;
        
        [self.navigationController pushViewController:diaryVC animated:YES];
        
        
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"WorldDetailTableViewCell";
    
    WorldDetailTableViewCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    // 取消点击置灰效果
    detailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    detailCell.cellDelegate = self;
    
    ForeignModel *foreignModel = [self.myArray objectAtIndex:indexPath.row];
    
    detailCell.foreignModel = foreignModel;
    
    return detailCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300 * HEI;
}

-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
